//
//  TodayViewModel.m
//  Water
//
//  Created by yh on 2017/11/30.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "TodayViewModel.h"
#import "WaterAPI.h"
#import <XMLDictionary.h>
#import <NSDate+GBExt.h>
#import <NSDate+YYAdd.h>
@implementation TodayViewModel


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.total = [[AppClient shared].todayTarget doubleValue];
    }
    return self;
}


- (void)getWeather{
    [WaterAPI getWeater:@"南宁" progress:nil success:^(NSURLSessionDataTask *task, id responesObject) {
       NSDictionary *weatherInfo = [[XMLDictionaryParser sharedInstance] dictionaryWithData:responesObject];
        WeatherInfo *info = [[WeatherInfo alloc]init];
        info.city = weatherInfo[@"city"];
        info.temperature = weatherInfo[@"wendu"];
        info.humidity = weatherInfo[@"shidu"];
        info.direction = weatherInfo[@"fengxiang"];
        info.wind = weatherInfo[@"fengli"];
        
        self.weatherInfo = info;
        NSLog(@"%@",weatherInfo);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}


- (NSMutableArray<WaterCellModel *> *)waters{
    if(!_waters){
        _waters = [[NSMutableArray alloc]init];
        
        {
            WaterCellModel *model = [[WaterCellModel alloc]init];
            model.imageName = @"bot1";
            model.amount = 30;
            [_waters addObject:model];
        }
        
        {
            WaterCellModel *model = [[WaterCellModel alloc]init];
            model.imageName = @"bot2";
            model.amount = 80;
            [_waters addObject:model];
        }
        
        {
            WaterCellModel *model = [[WaterCellModel alloc]init];
            model.imageName = @"bot3";
            model.amount = 150;
            [_waters addObject:model];
        }
        
        {
            WaterCellModel *model = [[WaterCellModel alloc]init];
            model.imageName = @"bot4";
            model.amount = 200;
            [_waters addObject:model];
        }
        
    }
    
    return _waters;
}

- (void)drinkWith:(WaterCellModel *)model{
    DrinkRecord *record = [[DrinkRecord alloc]init];
    record.amount = model.amount;
    record.total = self.total;
    record.time = [[NSDate date] stringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    //本地添加
    [[AppDataBaseManager shareManager] addDrinkRecord:record];
    //服务器添
    [WaterAPI addDrinkRecord:record progress:nil success:^(ApiResponse *response) {
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)setCurrent:(int)current{
    _current = current;
}

@end
